/*
 * Copyright (C) 2017 Zhengzhou BYCDAO Technology CO.,LTD.
 * All rights reserved.
 * Official Web Site: http://www.bycdao.com.
 * Developer Web Site: http://open.bycdao.com.
 */

package com.grandtech.oss.filter;

import com.grandtech.oss.common.annotation.NotLogin;
import com.grandtech.oss.common.conf.Consts;
import com.grandtech.oss.exception.UserNotLoginException;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/***
 *
 * @author <a href="mailto:yanggh@foxmail.com">yanggh@foxmail.com</a>
 * 2018/09/21 13:35
 */
@Deprecated
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        boolean flag = true;
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            NotLogin login = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), NotLogin.class);
            if (login == null) {
                HttpSession session = request.getSession();
                if (session.getAttribute(Consts.Admin_User_Session) == null) {
                    flag = false;
                    throw new UserNotLoginException("用户未登录");
                }
            }
        }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
